Completed
Push — master ( b7fd23...df38cf )
by Alejandro
27s queued 10s
created

CreateShortUrlResult.js ➔ render   A

Complexity

Conditions 3

Size

Total Lines 38
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 38
ccs 8
cts 8
cp 1
rs 9.064
c 0
b 0
f 0
cc 3
crap 3
1
import { faCopy as copyIcon } from '@fortawesome/free-regular-svg-icons';
2
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3
import { isNil } from 'ramda';
4
import React, { useEffect } from 'react';
5
import { CopyToClipboard } from 'react-copy-to-clipboard';
6
import { Card, CardBody, Tooltip } from 'reactstrap';
7
import PropTypes from 'prop-types';
8
import { createShortUrlResultType } from '../reducers/shortUrlCreation';
9
import './CreateShortUrlResult.scss';
10
11 1
const propTypes = {
12
  resetCreateShortUrl: PropTypes.func,
13
  error: PropTypes.bool,
14
  result: createShortUrlResultType,
15
};
16
17 1
const CreateShortUrlResult = (useStateFlagTimeout) => {
18 1
  const CreateShortUrlResultComp = ({ error, result, resetCreateShortUrl }) => {
19 4
    const [ showCopyTooltip, setShowCopyTooltip ] = useStateFlagTimeout();
20
21 4
    useEffect(() => {
22
      resetCreateShortUrl();
23
    }, []);
24
25 4
    if (error) {
26 1
      return (
27
        <Card body color="danger" inverse className="bg-danger mt-3">
28
          An error occurred while creating the URL :(
29
        </Card>
30
      );
31
    }
32
33 3
    if (isNil(result)) {
34 1
      return null;
35
    }
36
37 2
    const { shortUrl } = result;
38
39 2
    return (
40
      <Card inverse className="bg-main mt-3">
41
        <CardBody>
42
          <b>Great!</b> The short URL is <b>{shortUrl}</b>
43
44
          <CopyToClipboard text={shortUrl} onCopy={setShowCopyTooltip}>
45
            <button
46
              className="btn btn-light btn-sm create-short-url-result__copy-btn"
47
              id="copyBtn"
48
              type="button"
49
            >
50
              <FontAwesomeIcon icon={copyIcon} /> Copy
51
            </button>
52
          </CopyToClipboard>
53
54
          <Tooltip placement="left" isOpen={showCopyTooltip} target="copyBtn">
55
            Copied!
56
          </Tooltip>
57
        </CardBody>
58
      </Card>
59
    );
60
  };
61
62 1
  CreateShortUrlResultComp.propTypes = propTypes;
63
64 1
  return CreateShortUrlResultComp;
65
};
66
67
export default CreateShortUrlResult;
68